<template>
  <div class="wrapper" ref="emmm">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import Pullup from '@better-scroll/pull-up'

BScroll.use(Pullup)

export default {
  name: "Scroll",
  props: {
    probeType: {
      type: Number,
      default: 0
    },
    pullUpLoad: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      scroll: null,
    }
  },
  mounted() {
    // 1.创建BScroll对象
    this.scroll = new BScroll(this.$refs.emmm, {
      click: true,
      scrollY: true,

      observeDOM: true,
      observeImage: 100,

      probeType: this.probeType,
      // probeType: 3,

      pullUpLoad: this.pullUpLoad,
      pullUpLoad: true,

      baps: true,
    })

    // 2.监听滚动的位置
    this.scroll.on('scroll', (position) => {
      this.$emit('scroll', position)
      // console.log(position);
    })

    // 3.监听上拉事件
    this.scroll.on('pullingUp', () => {
      this.$emit('pullingUp')
      // 等待数据请求完成，并且重置上拉事件
      setTimeout(() => {
        this.scroll.finishPullUp()
      }, 1000)
      console.log('上拉加载')
    })
  },
  methods: {
    // home回传的BackTop组件被点击事件
    scrollTo1(x, y, time = 300) {
      this.scroll.scrollTo(x, y, time)

    },
    finishPullUp() {
      // this.scroll && this.scroll.finishPullUp()
      this.scroll.finishPullUp()
    },
    // refresh() {
    //   this.scroll.refresh()
    // }
    getScrollY() {
      return this.scroll ? this.scroll.y : 0
    }
  }
}
</script>

<style scoped>

</style>